using UnityEngine;
using UnityEngine.Serialization;

public class Level02Manager : MonoBehaviour
{
    [FormerlySerializedAs("sc")] [Header("滑轮引用")] [SerializeField] private BarController bc;

    [Header("检查区域引用")] [SerializeField] private Transform checkCenterPos;

    [Header("判定半径")] [SerializeField] private int checkRadius;

    private bool _finishedCheck;

    private void Start()
    {
        PlayerController.Instance.HidePlayer();
        checkCenterPos.localScale = new Vector3(checkRadius * 2, checkRadius * 2, 1);
    }

    private void Update()
    {
        if (bc.GetFinishedDrag())
        {
            if (!_finishedCheck)
            {
                if (CheckIsInArea())
                {
                    Debug.Log("Clearance!");
                    MapManager.Instance.SetLastSceneIndex(2);
                    //UIManager.Instance.ShowClearancePanel();
                    GameManager.Instance.Clearance(2);
                }
                else
                {
                    Debug.Log("Fail!");
                }
                _finishedCheck = true;
            }
        }
        else
        {
            _finishedCheck = false;
        }
    }

    private bool CheckIsInArea()
    {
        return Vector2.SqrMagnitude(checkCenterPos.position - bc.transform.position) <= checkRadius * checkRadius
            && Mathf.Abs(bc.transform.eulerAngles.z) <= 5f;
    }
}
